<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="renderer" content="webkit">
  <!-- import CSS -->
  <link rel="stylesheet" href="/style/default/index.css">
  <link rel="stylesheet" href="/style/default/my.css">
  <style>
  </style>
</head>

<body style="background-color:#f8f8f8;">
<div id="app">

  <!--目录信息dialog-->
  <el-dialog title="参数信息" :visible.sync="dialogSubmenuVisible" :close-on-click-modal="false" width="50%">
    <div style="height:220px;overflow-y:auto;">
      <el-form ref="submenuForm" :model="submenuData" :rules="rules" :disabled="dialogSubmenuDisabled"
               label-width="120px" size="mini" style="margin-right: 30px;">
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="submenuData.name"></el-input>
          </el-form-item>
          <el-form-item label="序号(高-低)">
            <el-input-number v-model="submenuData.sort" :min="1" :max="9999"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数名" prop="option_key">
            <el-input v-model="submenuData.option_key"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="submenuData.status" placeholder="请选择">
              <el-option v-for="item in optionData.status" :key="item.value" :label="item.name" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input type="textarea" v-model="submenuData.ext" rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogSubmenuVisible = false">取 消</el-button>
      <el-button @click="submitEditSubmenu" type="primary" :disabled="dialogSubmenuDisabled">确 定
      </el-button>
    </div>
  </el-dialog>

  <!--菜单信息dialog-->
  <el-dialog title="选项信息" :visible.sync="dialogItemVisible" :close-on-click-modal="false" width="50%">
    <div style="height:320px;overflow-y:auto;">
      <el-form ref="itemForm" :model="itemData" :rules="rules" :disabled="dialogItemDisabled" label-width="120px"
               size="mini" style="margin-right: 30px;">
        <el-col :span="12">
          <el-form-item label="选项名称" prop="name">
            <el-input v-model="itemData.name"></el-input>
          </el-form-item>
          <el-form-item label="值(数字)">
            <el-input-number v-model="itemData.option_value" :min="-9999" :max="9999"></el-input-number>
          </el-form-item>
          <el-form-item label="图标">
            <el-input v-model="itemData.icon"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="itemData.status" placeholder="请选择">
              <el-option v-for="item in optionData.status" :key="item.value" :label="item.name" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标记">
            <el-input v-model="itemData.tag"></el-input>
          </el-form-item>
          <el-form-item label="值(字符)">
            <el-input v-model="itemData.option_key"></el-input>
          </el-form-item>
          <el-form-item label="序号(高-低)">
            <el-input-number v-model="itemData.sort" :min="1" :max="9999"></el-input-number>
          </el-form-item>
          <el-form-item label="颜色">
            <el-color-picker v-model="itemData.color"></el-color-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input type="textarea" v-model="itemData.ext" rows="4"></el-input>
          </el-form-item>
        </el-col>
        </el-col>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogItemVisible = false">取 消</el-button>
      <el-button @click="submitEditItem" type="primary" :disabled="dialogItemDisabled">确 定</el-button>
    </div>
  </el-dialog>

  <el-card class="box-card" :body-style="{padding:'10px'}">
    <div class="clearfix" style="padding: 6px;border-bottom:1px solid #eee;margin-bottom: 10px;height:26px;">
      <span class="title-group-align">系统选项参数</span>

      <div class="btn-group-align">
        <el-button-group>
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="showAddSubmenu">添加新的参数</el-button>
        </el-button-group>
      </div>
    </div>

    <div>

      <el-table  v-loading="tableLoading" ref="optionParamsTable" :data="optionParamsData" style="width: 100%;"
                row-key="id" border :height="tableHeight" :max-height="tableHeight" size="mini"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :cell-style="{padding:'5px'}"
                lazy :load="loadTableData" @row-dblclick="editScope">
        <el-table-column prop="name" label="名称" width="240" fixed="left">
          <template slot-scope="scope">
            <el-link :underline="false" v-if="scope.row.pid == 0" type="primary" >
                {{scope.row.name}}
            </el-link>
            <el-link :underline="false" v-if="scope.row.pid > 0" type="warning" :icon="scope.row.icon" >
                {{scope.row.name}}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="option_key" label="值(字符)" width="140">
        </el-table-column>
        <el-table-column prop="option_value" label="值(数字)" width="80">
        </el-table-column>
        <el-table-column prop="sort" label="序号" width="60">
        </el-table-column>
        <el-table-column prop="color" label="颜色" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.color" :style="{ color: scope.row.color }">█ {{scope.row.color}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="icon" label="图标" width="140">
          <template slot-scope="scope">
            <span v-if="scope.row.icon" :class="scope.row.icon"> {{scope.row.icon}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="tag" label="标记">
        </el-table-column>
        <el-table-column prop="ext" label="备注" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80">
          <template slot-scope="scope">
              <span v-for="item in optionData.status" v-if="scope.row.status === item.value"
                    :style="{color:item.color}">{{item.name}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="140">
          <template slot-scope="scope">
            <el-button v-if="scope.row.pid === 0" @click="addItem(scope.row)" type="text" class="my-color-warning"
                       size="small">添加选项
            </el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>
  </el-card>

</div>
</body>
<script src="http://cdn.staticfile.org/vue/2.6.10/vue.min.js"></script>
<script src="http://cdn.staticfile.org/element-ui/2.12.0/index.js"></script>
<script src="http://cdn.staticfile.org/axios/0.19.0/axios.min.js"></script>
<script src="/js/validate.js"></script>
<script src="/js/my_util.js"></script>

<script>
  var marginHeight = 91; // 高差数

  var vm = new Vue({
    el: '#app',
    data: function () {
      return {
        tableHeight: window.innerHeight - marginHeight, // 表格高，当窗口改变大小的时候通过 pageResize 修改该值
        tableLoading: true,           // 是否显示加载表格数据加载中画面
        optionParamsData: [],         // 参数数据
        dialogSubmenuVisible: false,  // 是否显示目录信息对话框
        dialogSubmenuDisabled: false, // 目录信息对话框是否禁止编辑
        dialogItemVisible: false,     // 是否显示菜单信息对话框
        dialogItemDisabled: false,    // 菜单信息对话框是否禁止编辑
        submenuData: {},              // 目录数据
        itemData: {},                 // 菜单数据
        treeMaps: new Map(),          // 保存懒加载的参数

        // 表单数据验证
        rules: {
          name: [
            {required: true, message: '该值不能为空', trigger: 'blur'},
            {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
          ],
          option_key: [
            {required: true, message: '该值不能为空', trigger: 'blur'},
            {min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur'}
          ]
        },
      }
    },
    computed: {
      // 从缓存中读取
      optionData: function () {
        var optionDataStr = sessionStorage.getItem('optionData');
        return JSON.parse(optionDataStr || "{}");
      }
    },
    mounted() {
      // 当窗口大小发生改变的时候系统自动调用
      window.onresize = () => {
        this.pageResize();
      }
    },
    methods: {
      // 改变表格高度
      pageResize() {
        this.tableHeight = window.innerHeight - marginHeight;
      },

      // 树形表格中加载数据的方法
      loadTableData(tree, treeNode, resolve) {
        // 保存懒加载的参数
        const pid = tree.id;
        this.treeMaps.set(pid, {tree, treeNode, resolve})

        // 获取下级数据
        axios.get('/admin/base/' + tree.id + '/show/sys_option_params!children.bu')
          .then(function (response) {
            if (response.data.code > 0) {
              resolve(response.data.bundle);
            } else if (response.data.code == 0) {
              resolve([]);
            } else {
              vm.$message.error(response.data.msg);
            }
          })
          .catch(function (error) {
            vm.$message.error('获取数据失败！' + error);
          })
      },

      // 重新加载父级树列表
      reloadTree(pid) {
        if (this.treeMaps.has(pid)) {
          const {tree, treeNode, resolve} = this.treeMaps.get(pid)
          this.$set(this.$refs.optionParamsTable.store.states.lazyTreeNodeMap, pid, [])
          this.loadTableData(tree, treeNode, resolve);
        }
      },

      // 新增菜单
      addItem(row, type) {
        this.itemData = {pid:row.id,status:1};
        this.dialogItemVisible = true;
        this.dialogItemDisabled = false;
      },

      // 显示目录
      showAddSubmenu() {
        this.submenuData = {pid:0,status:1};
        this.dialogSubmenuVisible = true;
        this.dialogSubmenuDisabled = false;
      },

      // 选择项目
      selectScope(row) {
        if (row.pid === 0) {
          this.dialogSubmenuVisible = true;
          this.dialogSubmenuDisabled = true;
          this.submenuData = JSON.parse(JSON.stringify(row));
        } else if (row.pid > 0) {
          this.dialogItemVisible = true;
          this.dialogItemDisabled = true;
          this.itemData = JSON.parse(JSON.stringify(row));
        }
      },

      // 编辑项目
      editScope(row) {
        if (row.pid === 0) {
          this.dialogSubmenuVisible = true;
          this.dialogSubmenuDisabled = false;
          this.submenuData = JSON.parse(JSON.stringify(row));
        } else if (row.pid > 0) {
          this.dialogItemVisible = true;
          this.dialogItemDisabled = false;
          this.itemData = JSON.parse(JSON.stringify(row));
        }
      },

      // 提交对目录的修改
      submitEditSubmenu(e) {
        this.$refs.submenuForm.validate((valid) => {
          if (valid) {
            delete this.submenuData.hasChildren  // 因为数据库里没有该字段。所以需要先删除,才能确保数据保存成功
            axios.post('/admin/base/edit/sys_option_params.bu', this.submenuData)
              .then(function (response) {
                if (response.data.code > 0) {
                  vm.$message.success(response.data.msg);
                  vm.dialogSubmenuVisible = false;
                  loadInitData(); // 重新加载数据
                } else {
                  vm.$message.error(response.data.msg);
                }
              })
              .catch(function (error) {
                vm.$message.error('出错：' + error);
              });
          } else {
            vm.$message.error('错误：提交失败，请检查表单数据是否符合要求！');
            return false;
          }
        });
      },

      // 提交对菜单项的修改
      submitEditItem(e) {
        this.$refs.itemForm.validate((valid) => {
          if (valid) {
            delete this.itemData.hasChildren  // 因为数据库里没有该字段。所以需要先删除,才能确保数据保存成功
            axios.post('/admin/base/edit/sys_option_params.bu', this.itemData)
              .then(function (response) {
                if (response.data.code > 0) {
                  vm.$message.success(response.data.msg);
                  vm.dialogItemVisible = false;

                  vm.reloadTree(vm.itemData.pid) // 重新加载父数据
                } else {
                  vm.$message.error(response.data.msg);
                }
              })
              .catch(function (error) {
                vm.$message.error('出错：' + error);
              });
          } else {
            vm.$message.error('错误：提交失败，请检查表单数据是否符合要求！');
            return false;
          }
        });
      }
    },
  });

  // 自动执行，读取菜单数据等
  function loadInitData() {
    axios.get('/admin/base/0/show/sys_option_params!children.bu')
      .then(function (response) {
        if (response.data.code > 0) {
          vm.optionParamsData = response.data.bundle;
          vm.tableLoading = false;
        } else if (response.data.code == 0) {
          vm.$message.error("没有数据");
          vm.tableLoading = false;
        } else {
          vm.$message.error(response.data.msg);
        }
      })
      .catch(function (error) {
        vm.$message.error('获取数据失败！' + error);
      })
  };

  loadInitData();


</script>

</html>